/**
  **************************************************************************
  * @file     main.c
  * @version  v2.1.0
  * @date     2022-06-09
  * @brief    main program
  **************************************************************************
  *                       Copyright notice & Disclaimer
  *
  * The software Board Support Package (BSP) that is made available to
  * download from Artery official website is the copyrighted work of Artery.
  * Artery authorizes customers to use, copy, and distribute the BSP
  * software and its related documentation for the purpose of design and
  * development in conjunction with Artery microcontrollers. Use of the
  * software is governed by this copyright notice and the following disclaimer.
  *
  * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
  * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
  * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
  * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
  * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
  *
  **************************************************************************
  */

#include "at32f403a_407_board.h"
#include "at32f403a_407_clock.h"
#include "at32project_header.h"

#include "apc_sct.h"
#include "apc_key_func.h"
#include "apc_fw_info.h"
#include "core_usb_port.h"



/**
  * @brief  Crate User Application Function
  */
void display_fw_info(void)
{
	char fw_buf[50];
		
	SEGGER_RTT_printf(0, "****************************\r\n");
	sprintf(fw_buf , "  Master FW Info : %d.%d.%d \r\n",
		DEVICE_FW_MAJOR_VERSION,
		DEVICE_FW_MINOR_VERSION,
		DEVICE_FW_MICRO_VERSION);
	
	SEGGER_RTT_printf(0, "%s",fw_buf);
	SEGGER_RTT_printf(0, "****************************\r\n");
}



/**
  * @brief  Crate User Application Function
  */
void create_task_function(void *pvParameters)
{
	// 创建 SPI 数据收发监听线程
	apc_sct_task_create();
	
	apc_key_scan_create();
	
	// 退出此线程
	vTaskDelete(NULL);
}

/**
  * @brief  main function.
  * @param  none
  * @retval none
  */
int main(void)
{
	nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);

	SEGGER_RTT_ConfigUpBuffer(0, NULL, NULL, 0, SEGGER_RTT_MODE_NO_BLOCK_SKIP);

	system_clock_config();

	at32_board_init();

	// 显示当前固件信息.
	display_fw_info();
	
	/* enter critical */
	taskENTER_CRITICAL();

	/* create main task */
	if(xTaskCreate((TaskFunction_t )create_task_function,
				 (const char*    )"create_task",
				 (uint16_t       )1024,
				 (void*          )NULL,
				 (UBaseType_t    )(configMAX_PRIORITIES - 1),
				 (TaskHandle_t*  )NULL) != pdPASS)
	{
		SEGGER_RTT_printf(0, "Create task failed\r\n");
	}

	/* exit critical */
	taskEXIT_CRITICAL();

	/* start scheduler */
	vTaskStartScheduler();

	while(1);
}

/**
  * @}
  */

void HardFault_Handler(void)
{
  /* go to infinite loop when hard fault exception occurs */
  while(1)
  {
  }
}

